package com.zhy;

import com.intellagric.common.utils.JsonUtils;
import com.intellagric.controller.upload.Upload;
import com.kennycason.kumo.CollisionMode;
import com.kennycason.kumo.WordCloud;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.bg.CircleBackground;
import com.kennycason.kumo.bg.PixelBoundryBackground;
import com.kennycason.kumo.font.KumoFont;
import com.kennycason.kumo.font.scale.SqrtFontScalar;
import com.kennycason.kumo.nlp.FrequencyAnalyzer;
import com.kennycason.kumo.nlp.tokenizers.ChineseWordTokenizer;
import com.kennycason.kumo.palette.LinearGradientColorPalette;
import jdk.nashorn.internal.parser.JSONParser;

import java.awt.*;
import java.io.IOException;
import java.util.List;

/**
 * @Author: cwq
 * @Date: 2019/2/24 21:18
 * @Description:
 */
public class WorldCloud {
    public static void main(String[] args) throws IOException {

        long startTime=System.currentTimeMillis();

        //建立词频分析器，设置词频，以及词语最短长度，此处的参数配置视情况而定即可
        FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
        frequencyAnalyzer.setWordFrequenciesToReturn(50);
        frequencyAnalyzer.setMinWordLength(2);

        String test = "";

        //引入中文解析器
        frequencyAnalyzer.setWordTokenizer(new ChineseWordTokenizer());
        //指定文本文件路径，生成词频集合
        final List<WordFrequency> wordFrequencyList = frequencyAnalyzer.load("D:\\MyProjects\\intellagric\\intellagric-manager-web\\src\\main\\resources\\wc.txt");
        //设置图片分辨率
        Dimension dimension = new Dimension(600,600);
        //此处的设置采用内置常量即可，生成词云对象
        WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
        //设置边界及字体
        wordCloud.setPadding(2);
        java.awt.Font font = new java.awt.Font("STSong-Light", 2, 20);
        //设置词云显示的三种颜色，越靠前设置表示词频越高的词语的颜色
        wordCloud.setColorPalette(new LinearGradientColorPalette(Color.RED, Color.BLUE, Color.GREEN, 30, 30));
        wordCloud.setKumoFont(new KumoFont(font));
        //设置背景色
        wordCloud.setBackgroundColor(new Color(255,255,255));
        //设置背景图片
        //wordCloud.setBackground(new PixelBoundryBackground("D:\\MyProjects\\intellagric\\intellagric-manager-web\\src\\main\\resources\\dog.jpg"));
        //设置背景图层为圆形
        wordCloud.setBackground(new CircleBackground(300));
        wordCloud.setFontScalar(new SqrtFontScalar(12, 45));

        //生成词云
        wordCloud.build(wordFrequencyList);
        System.out.println(JsonUtils.objectToJson(wordFrequencyList).replaceAll("word", "name").replaceAll("frequency", "value"));
        //wordCloud.writeToFile("D:\\MyProjects\\intellagric\\intellagric-manager-web\\src\\main\\resources\\wy.png");
        /*wordCloud.build(wordFrequencyList);
        String json = new Upload().uploadPic(wordCloud.getBufferedImage(), "png");
        System.out.println(json);*/

        long endTime=System.currentTimeMillis();

        long interval=endTime-startTime;
        System.out.println(interval);
    }
}
